﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Extensions;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Common;
using TaoPortal.Common.Utility;
using TaoPortal.Entity;


namespace TaoPortal.Areas.Admin.Models
{
    public class ArticleModel : BaseModel
    {
        public AddArticle AddArticle { get; set; }
        public ArticleSearcher Searcher { get; set; }
        /// <summary>
        /// 分类选择框数据
        /// </summary>
        public string ArticleClass { get; set; }

        public string ImgListStr { get; set; }

        public ArticleModel()
        {
            Searcher = new ArticleSearcher();
            ArticleClass = "[]";
            ImgListStr = "[]";
        }

        public List<TreeData> GetClass(string parentid)
        {
            return GetClassByPID(TaoPortal.Common.SysEnum.SysClassType.ArticleType, parentid);
        }

        public void InitArticle(string id)
        {
            var rp = new ArticleRepository();
            var a = rp.GetFullArtileByID(id);
            AddArticle = new AddArticle
            {
                ID = a.TP_Article.ID,
                ArticleClass = a.TP_ArticleClass.FirstOrDefault().ClassID,
                AddDateTime = a.TP_Article.AddDateTime,
                AKey = a.TP_Article.AKey,
                Allowreply = a.TP_Article.Allowreply,
                Author = a.TP_Article.Author,
                Source = a.TP_Article.Source,
                SourceUrl = a.TP_Article.SourceUrl,
                Status = a.TP_Article.Status,
                Summary = a.TP_Article.Summary,
                Title = a.TP_Article.Title,
                TitleImage = a.TP_Article.TitleImage,
                FID = a.TP_Article.FID,
                FIntegral = a.TP_Article.FIntegral
            };
            if (!string.IsNullOrEmpty(AddArticle.FID))
            {
                var frp = new FileRepository();
                var file = frp.GetSimpleFile(AddArticle.FID);
                if (file != null)
                    AddArticle.FName = file.FName;
            }
            if (a.TP_ArticleBody != null)
            {
                for (int i = 0; i < a.TP_ArticleBody.Count(); i++)
                {
                    if (i == 0)
                    {
                        AddArticle.Body = a.TP_ArticleBody.ToList()[i].ArticleBody;
                    }
                    else
                    {
                        AddArticle.Body += "_" + a.TP_ArticleBody.ToList()[i].ArticleBody;
                    }
                }
            }
            if (a.TP_Tag != null)
            {
                for (int i = 0; i < a.TP_Tag.Count(); i++)
                {
                    if (i == 0)
                    {
                        AddArticle.Tag = a.TP_Tag.ToList()[i].TagName;
                    }
                    else
                    {
                        AddArticle.Tag += "," + a.TP_Tag.ToList()[i].TagName;
                    }
                }
            }
            if (a.TP_ArticleImg != null && a.TP_ArticleImg.Count() > 0)
            {

                var imgList = a.TP_ArticleImg.Select(x => new
                {
                    FileID = x.FileID,
                    FileName = x.FileName,
                    Describe = x.Describe,
                    Sort = x.Sort
                }).ToList();

                ImgListStr = Common.Utility.JsonSerialization.SerializeObject(imgList);
            }
        }

        public void InitAddData()
        {
            var classList = GetClassByType(TaoPortal.Common.SysEnum.SysClassType.ArticleType);
            ArticleClass = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(classList);
            AddArticle = new AddArticle
            {
                //分类为必填项，默认选中第一个分类
                ArticleClass = (classList != null && classList.FirstOrDefault() != null) ? classList.FirstOrDefault().id : "",
                AddDateTime = DateTime.Now.Date
            };
        }
        public void InitID()
        {
            if (AddArticle == null)
                AddArticle = new Models.AddArticle { ID = Guid.NewGuid().ToString() };
            else
                AddArticle.ID = Guid.NewGuid().ToString();
        }
        public PagedList<ArticleList> GetArticleList()
        {
            var rp = new ArticleRepository();
            var data = rp.GetArticleList(Searcher.Title, Searcher.AKey, Searcher.Status, Searcher.AClass, Searcher.StartDate, Searcher.EndDate, PageIndex, PageSize);
            foreach (var item in data)
            {
                item.Status = HttpContextCache.GetParamValue(typeof(SysEnum.ArticleStatus).Name, item.Status);
            }
            return data;
        }

        public void Add()
        {
            var article = new TP_Article
            {
                ID = AddArticle.ID,
                Title = AddArticle.Title,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                UpdateBy = SessionEnt.UserName,
                Summary = AddArticle.Summary,
                Status = AddArticle.Status,
                SourceUrl = AddArticle.SourceUrl,
                Source = AddArticle.Source,
                AKey = AddArticle.AKey,
                TitleImage = AddArticle.TitleImage,
                AddDateTime = AddArticle.AddDateTime,
                CreateBy = SessionEnt.UserName,
                Author = AddArticle.Author,
                Allowreply = AddArticle.Allowreply,
                FID = AddArticle.FID,
                FIntegral = AddArticle.FIntegral
            };
            var aClass = new TP_ArticleClass
            {
                ID = Guid.NewGuid().ToString(),
                ArticleID = article.ID,
                ClassID = AddArticle.ArticleClass
            };
            List<TP_ArticleBody> bodyList = new List<TP_ArticleBody>();
            if (!string.IsNullOrEmpty(AddArticle.Body))
            {
                var bodys = AddArticle.Body.Split('-');
                for (int i = 0; i < bodys.Length; i++)
                {
                    bodyList.Add(new TP_ArticleBody
                    {
                        ID = Guid.NewGuid().ToString(),
                        ArticleID = article.ID,
                        ArticleBody = bodys[i],
                        PageIndex = i + 1
                    });
                }
            }
            List<TP_Tag> tagList = new List<TP_Tag>();
            if (!string.IsNullOrEmpty(AddArticle.Tag))
            {
                AddArticle.Tag = AddArticle.Tag.Replace(',', '，');
                var tags = AddArticle.Tag.Split('，');
                for (int i = 0; i < tags.Length; i++)
                {
                    tagList.Add(new TP_Tag
                    {
                        ID = Guid.NewGuid().ToString(),
                        TagName = tags[i]
                    });
                }
            }
            List<TP_ArticleImg> imgs = new List<TP_ArticleImg>();
            if (AddArticle.ArticleImg != null)
            {
                foreach (var item in AddArticle.ArticleImg)
                {
                    imgs.Add(new TP_ArticleImg
                    {
                        ID = Guid.NewGuid().ToString(),
                        ArticleID = article.ID,
                        FileID = item.FileID,
                        FileName = item.FileName,
                        Describe = item.Describe,
                        Sort = item.Sort
                    });
                }
            }
            var rp = new ArticleRepository();
            rp.AddArticle(article, aClass, bodyList, tagList, imgs);
        }

        public void Edit()
        {
            var article = new TP_Article
            {
                ID = AddArticle.ID,
                Title = AddArticle.Title,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                UpdateBy = SessionEnt.UserName,
                Summary = AddArticle.Summary,
                Status = AddArticle.Status,
                SourceUrl = AddArticle.SourceUrl,
                Source = AddArticle.Source,
                AKey = AddArticle.AKey,
                TitleImage = AddArticle.TitleImage,
                AddDateTime = AddArticle.AddDateTime,
                CreateBy = SessionEnt.UserName,
                Author = AddArticle.Author,
                Allowreply = AddArticle.Allowreply,
                FID = AddArticle.FID,
                FIntegral = AddArticle.FIntegral
            };
            var aClass = new TP_ArticleClass
            {
                ID = Guid.NewGuid().ToString(),
                ArticleID = article.ID,
                ClassID = AddArticle.ArticleClass
            };
            List<TP_ArticleBody> bodyList = new List<TP_ArticleBody>();
            var bodys = AddArticle.Body.Split('-');
            for (int i = 0; i < bodys.Length; i++)
            {
                bodyList.Add(new TP_ArticleBody
                {
                    ID = Guid.NewGuid().ToString(),
                    ArticleID = AddArticle.ID,
                    ArticleBody = bodys[i],
                    PageIndex = i + 1
                });
            }
            List<TP_Tag> tagList = new List<TP_Tag>();
            AddArticle.Tag = AddArticle.Tag.Replace(',', '，');
            var tags = AddArticle.Tag.Split('，');
            for (int i = 0; i < tags.Length; i++)
            {
                tagList.Add(new TP_Tag
                {
                    ID = Guid.NewGuid().ToString(),
                    TagName = tags[i]
                });
            }
            List<TP_ArticleImg> imgs = new List<TP_ArticleImg>();
            if (AddArticle.ArticleImg != null)
            {
                foreach (var item in AddArticle.ArticleImg)
                {
                    imgs.Add(new TP_ArticleImg
                    {
                        ID = Guid.NewGuid().ToString(),
                        ArticleID = article.ID,
                        FileID = item.FileID,
                        FileName = item.FileName,
                        Describe = item.Describe,
                        Sort = item.Sort
                    });
                }
            }
            var rp = new ArticleRepository();
            rp.UpdateArticle(article, aClass, bodyList, tagList, imgs);
        }

        public void Delete(string id)
        {
            var rp = new ArticleRepository();
            rp.DeleteArticle(id);
        }
    }

    public class ArticleSearcher
    {
        public string Title { get; set; }
        public string AKey { get; set; }
        public string AClass { get; set; }
        public string Status { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
    }



    public class AddArticle
    {
        public string ID { get; set; }
        [Required]
        public string Title { get; set; }
        public string AKey { get; set; }
        public string TitleImage { get; set; }
        public string Body { get; set; }
        public string Summary { get; set; }
        [Required]
        public string ArticleClass { get; set; }
        public bool Allowreply { get; set; }
        public string Author { get; set; }
        public string SourceUrl { get; set; }
        public string Source { get; set; }
        public Nullable<System.DateTime> AddDateTime { get; set; }
        public string Status { get; set; }
        public string Tag { get; set; }
        public List<TP_ArticleImg> ArticleImg { get; set; }
        public string FID { get; set; }
        public string FName { get; set; }
        public Nullable<int> FIntegral { get; set; }

    }
}